//
//  CustomBaseMessageTableViewCell.m
//  WeWin
//
//  Created by lifayu on 2018/9/26.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "CustomBaseMessageTableViewCell.h"
#import <UIView+BlocksKit.h>
@implementation CustomBaseMessageTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self addOwnViews];
    }
    return self;
}


- (UILabel *)nickNameLabel{
    if(!_nickNameLabel){
        _nickNameLabel = [[UILabel alloc] init];
        _nickNameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
        _nickNameLabel.numberOfLines = 1;
    }
    return _nickNameLabel;
}



- (UIImageView *)portraitImageView{
    if(!_portraitImageView){
        _portraitImageView = [[UIImageView alloc] init];
        _portraitImageView.image = [UIImage imageNamed:@"logo"];
        _portraitImageView.size = CGSizeMake(24, 24);
        _portraitImageView.cornerRadius = 12;
        _portraitImageView.userInteractionEnabled = YES;
        FlyWeakSelf
        [_portraitImageView bk_whenTapped:^{
        }];
    }
    return _portraitImageView;
}

- (void)addOwnViews{
    [self.contentView addSubview:self.portraitImageView];
    [self.contentView addSubview:self.nickNameLabel];
}

- (void)setMessageModel:(CSModel *)messageModel{
    if(_messageModel != messageModel){
        _messageModel = messageModel;
    }
    [self configOwnViews];
}

- (void)configOwnViews{
    
    if([self.messageModel.poster isEqualToString:@"user"]){
        self.nickNameLabel.text = [NSString stringWithFormat:@"%@ %@",self.messageModel.createTime,[EJSUserModel userModel].name?[EJSUserModel userModel].name:[EJSUserModel userModel].phone];
        [self.portraitImageView setImageWithUrl:self.messageModel.userAvatar];
    }else{
        self.nickNameLabel.text = [NSString stringWithFormat:@"%@ %@",self.messageModel.adminName?self.messageModel.adminName:@"一品客服",self.messageModel.createTime];
        [self.portraitImageView setImageWithUrl:self.messageModel.adminAvatar];
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.nickNameLabel sizeToFit];
    if([self.messageModel.poster isEqualToString:@"user"]){
        _portraitImageView.right = ScreenWidth - 24;
        _portraitImageView.bottom = self.height - 20;
        _nickNameLabel.top = _portraitImageView.bottom;
        _nickNameLabel.right = _portraitImageView.right;
        _nickNameLabel.height = 20;
    }else{
        _portraitImageView.left = 24;
        _portraitImageView.top = MessageCellPadding + 20;
        _nickNameLabel.top = MessageCellPadding;
        _nickNameLabel.left = 24;
        _nickNameLabel.height = 20;
    }
}

+ (CGFloat)cellHeightForMessageModel:(CSModel *)messageModel{
    if (messageModel.cellHeight > 0) {
        return messageModel.cellHeight;
    }
    CGFloat height = MessageCellPadding + 20;
    NSAttributedString *text = [[NSAttributedString alloc] initWithString:messageModel.content attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14]}];
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MaxBubbleViewWidth - 16*2, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
    height += rect.size.height + 20;

    return height;
}
@end

